package com.fin.zw.aiqas.controller;

import com.fin.zw.aiqas.annotation.CurrentUser;
import com.fin.zw.aiqas.entity.RuleType;
import com.fin.zw.aiqas.model.request.RuleTypeRequest;
import com.fin.zw.aiqas.model.session.SessionModel;
import com.fin.zw.aiqas.service.RuleTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;


@RestController
@Api(value = "规则分类", description = "规则分类")
@RequestMapping("/ruletype")
public class RuleTypeController {
    @Autowired
    private RuleTypeService ruleTypeService;

    @GetMapping("/fetchPage")
    @ApiOperation(value = "分页查询全部规则分类", notes = "分页查询全部规则分类")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", dataType = "int", paramType = "query", value = "页数（0..N）"),
            @ApiImplicitParam(name = "size", dataType = "int", paramType = "query", value = "每页大小."),
            @ApiImplicitParam(name = "sort", allowMultiple = true, dataType = "string", paramType = "query", value = "依据什么排序: 属性名(,asc|desc). "),
    })
    public ResponseEntity<Page<RuleType>> fetchPage(RuleTypeRequest request, Pageable pageable) {
        Page<RuleType> page = ruleTypeService.findAll(request, pageable);
        return ResponseEntity.ok().body(page);

    }

    @GetMapping(value = "/fetch/{id}")
    @ApiOperation(value = "根据ID查询规则分类", notes = "根据ID查询规则分类")
    public ResponseEntity<RuleType> fetchById(@PathVariable Long id) {
        RuleType ruleType = ruleTypeService.fetchById(id);
        return ResponseEntity.ok().body(ruleType);
    }

    @GetMapping(value = "/fetchList")
    @ApiOperation(value = "不分页全部规则分类", notes = "不分页全部规则分类")
    public ResponseEntity<List<RuleType>> fetchPage(RuleTypeRequest request) {
        List<RuleType> ruleTypes = ruleTypeService.findAll(request);
        return ResponseEntity.ok().body(ruleTypes);


    }

    @RequestMapping(value = "/update", method = {RequestMethod.PUT, RequestMethod.PATCH})
    @ApiOperation(value = "更新规则分类信息", notes = "更新规则分类信息")
    public ResponseEntity<String> update(@RequestBody RuleTypeRequest request, @ApiIgnore @CurrentUser SessionModel sessionModel) {
        ruleTypeService.updateRuleType(request, sessionModel);
        return ResponseEntity.ok().body("更新成功");
    }

    @DeleteMapping(value = "/delete/{id}")
    @ApiOperation(value = "删除规则分类", notes = "删除规则分类")
    public ResponseEntity<Void> deleteRule(@PathVariable("id") Long id) {
        ruleTypeService.deleteRuleType(id);
        return ResponseEntity.ok().build();
    }

    @PostMapping("/add")
    @ApiOperation(value = "增加规则分类信息", notes = "增加规则分类信息")
    public ResponseEntity<String> createRuleType(@RequestBody RuleTypeRequest requestBean,
                                                 @ApiIgnore @CurrentUser SessionModel sessionModel) {
        ruleTypeService.createRuleType(requestBean, sessionModel);
        return ResponseEntity.ok().body("操作成功");
    }
}
